﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Widgetsphere.Generator.Common.GeneratorFramework;

namespace Widgetsphere.VsCodeTools
{
	internal static class VersionHelper
	{
		public static bool CanConnect()
		{
			hydrateservice.MainService service = null;
			try
			{
				service = new hydrateservice.MainService();
				return service.IsLive();
			}
			catch (Exception ex)
			{
				return false;
			}
		}

		public static string GetLatestVersion()
		{
			hydrateservice.MainService service = null;
			try
			{
				service = new hydrateservice.MainService();
				hydrateservice.VersionNumber version = service.GetLatestVersion3(AddinAppData.Instance.Key, GetCurrentVersion());
				return version.Version;
			}
			catch (Exception ex)
			{
				return "(Unknown)";
			}
		}

		public static string GetCurrentVersion()
		{
			Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
			return version.Major + "." + version.Minor + "." + version.Build + "." + version.Revision;
		}

		public static bool ShouldCheck()
		{
			return (DateTime.Now.Subtract(AddinAppData.Instance.LastUpdateCheck).TotalDays >= 7);
		}

		public static bool NeedUpdate(string newVersion)
		{
			string currentVersion = GetCurrentVersion();
			try
			{
				Version versionNew = new Version(newVersion);
				Version versionNow = new Version(currentVersion);
				return (versionNow < versionNew);
			}
			catch (Exception ex)
			{
				return (newVersion != currentVersion);
			}

		}

	}
}
